package com.hsteach.service.third;

import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.exception.XgkTokenException;
import com.hsteach.common.utils.StringUtils;
import com.hsteach.framework.feign.ClientConstant;
import com.hsteach.framework.feign.client.xgk.PreschoolXgkClient;
import com.hsteach.framework.feign.client.xgk.dto.XgkListResp;
import com.hsteach.framework.feign.client.xgk.dto.XgkResp;
import com.hsteach.framework.feign.client.xgk.dto.ret.GetTeacherRet;
import com.hsteach.framework.feign.client.xgk.dto.ret.preschool.PreschoolGetTeacherRet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: PreschoolXgkService
 * Package: com.hsteach.service.third
 * Description:
 *
 * @author wjh
 * @version 1.0
 * @since 2024/3/29 15:21
 */
@Service
public class PreschoolXgkService {

    @Autowired
    private PreschoolXgkClient preschoolXgkClient;

    /**
     * 根据accessToken获取幼教班级教师通讯录信息
     * @param accessToken (必选) 大平台accessToken
     * @param classId 班级ID（可选），不传则查询当前登录用户可以看到的所有老师
     * @return
     */
    public List<PreschoolGetTeacherRet> getPreschoolTeacherByClassId(String accessToken, String classId) {
        XgkListResp<PreschoolGetTeacherRet> resp = preschoolXgkClient.getPreschoolTeacherByClassId(accessToken,classId);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }
    private boolean isSuccess(XgkResp resp) {
        if (StringUtils.isNotEmpty(resp.getMessage()) && resp.getMessage().indexOf("用户令牌己更新") != -1) {
            throw new XgkTokenException(resp.getMessage());
        }
        if (ClientConstant.XGK_ISCMS_SUCCESS.equals(resp.getCode())) {
            return true;
        }
        return false;
    }
}
